package net.cgsoft.widget;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by Youga on 2015/12/25.
 */
public class LoadFragment extends DialogFragment {

    public static final String TAG = "LoadFragment";
    public static final String LOAD = "LOAD";
    public static final String EMPTY = "EMPTY";
    public static final String ERROR = "ERROR";
    TextView messageView;
    LinearLayout loadBody;
    ImageView imageView;
    TextView tipsView;
    LinearLayout tipBody;


    public static LoadFragment newInstance(String message) {
        LoadFragment fragment = new LoadFragment();
        Bundle bundle = new Bundle();
        bundle.putString("message", message);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static LoadFragment newInstance(String type, String tips, int[] imagesState) {
        LoadFragment fragment = new LoadFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        bundle.putString("tips", tips);
        bundle.putIntArray("imagesState", imagesState);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Dialog dialog = getDialog();
        if (dialog != null) dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.fragment_load, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        messageView = (TextView) view.findViewById(R.id.message);
        loadBody = (LinearLayout) view.findViewById(R.id.load_body);
        tipBody = (LinearLayout) view.findViewById(R.id.tip_body);
        imageView = (ImageView) view.findViewById(R.id.imageView);
        tipsView = (TextView) view.findViewById(R.id.tips);
        String message = getArguments().getString("message");
        String type = getArguments().getString("type");
        String tips = getArguments().getString("tips");
        int[] imagesState = getArguments().getIntArray("imagesState");
        if (type == null) type = "default";
        switch (type) {
            case LOAD:
                loadBody.setVisibility(View.GONE);
                tipBody.setVisibility(View.VISIBLE);
                tipsView.setText(tips);
                imageView.setImageResource(imagesState[0]);
                break;
            case EMPTY:
                loadBody.setVisibility(View.GONE);
                tipBody.setVisibility(View.VISIBLE);
                tipsView.setText(tips);
                imageView.setImageResource(imagesState[1]);
                break;
            case ERROR:
                loadBody.setVisibility(View.GONE);
                tipBody.setVisibility(View.VISIBLE);
                tipsView.setText(tips);
                imageView.setImageResource(imagesState[2]);
                break;
            default:
                messageView.setText(message);
                loadBody.setVisibility(View.VISIBLE);
                tipBody.setVisibility(View.GONE);
                break;
        }
    }
}
